package cn.kailangtu.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import cn.kailangtu.myapplication.databinding.FragmentSecond1Binding;
import cn.kailangtu.myapplication.databinding.FragmentSecond2Binding;
import cn.kailangtu.myapplication.events.SerialData;
import cn.kailangtu.myapplication.fragment.BaseFragment;
import cn.kailangtu.myapplication.tools.AdbUtil;
import cn.kailangtu.myapplication.tools.DataClearUtils;
import cn.kailangtu.myapplication.tools.MediaPlayerUtils;

public class SecondFragment2 extends BaseFragment {

    private FragmentSecond2Binding binding;




    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = FragmentSecond2Binding.inflate(inflater, container, false);
        return binding.getRoot();


    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String t=format.format(new Date());
        binding.TextViewTime.setText(t);

        binding.buttonSet1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(SecondFragment2.this)
                        .navigate(R.id.action_SecondFragment2_to_FirstFragment);
            }
        });
        //MediaPlayerUtils.getInstance().playZhuanxiangEnd();

       // binding.dpSettingDateId.
        //new Date(binding.dpSettingDateId.getYear(),)

      binding.btnSave.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              setTime();
          }
      });


      binding.buttonDelete.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              deleteCount++;
              //Toast.makeText(getContext(),"数字："+deleteCount,Toast.LENGTH_LONG).show();
              if(deleteCount >= 10) {
                  deleteCount = 0;
                  DataClearUtils.DeleteData();
                  Toast.makeText(getContext(),"清除里程完毕",Toast.LENGTH_LONG).show();
              }
          }
      });

    }
    int deleteCount = 0;


    public void setTime()
    {
        Calendar calendar = new GregorianCalendar(binding.dpSettingDateId.getYear(),
                binding.dpSettingDateId.getMonth(),
                binding.dpSettingDateId.getDayOfMonth(),
                binding.tpSettingTimeId.getHour(),
                binding.tpSettingTimeId.getMinute());
        SimpleDateFormat format =  new SimpleDateFormat("MMddHHmmyyyy.ss", Locale.getDefault());
        String timeStr = format.format(calendar.getTimeInMillis());
        Log.d("TIMETIMETIME",timeStr);
        AdbUtil.run("date '"+timeStr+"'");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SerialData event){

        if(event.getAtime()!=null) {
            binding.TextViewTime.setText(event.getAtime());
        }
        // data=event.getA1();
        // Log.e("rrrrrrrr","uuuu"+data[0]);
    }
    public static int booleanToInt(boolean boolValue) {
        // 使用三元运算符将boolean转换为int
        return boolValue ? 1 : 0;
    }
    //BYTE转成bool
    public  boolean[] byteToBooleanArray(byte b) {
        boolean[] boolArray = new boolean[8];
        for (int i = 0; i < 8; i++) {
            // 通过位运算检查当前位是否为1
            boolArray[i] = ((b >> i) & 1) == 1;
        }
        return boolArray;
    }

    //合并成int
    public static int combineBytesToInt(byte byte1, byte byte2) {
        // 将byte1左移8位，空出的低位由byte2填充
        return (byte1 << 8) | (byte2 & 0xFF);
    }

    //EventBus  推出
    @Override
    public void onStop(){
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;


    }

}